package com.ysd.ziyuanshiyongtongji.service;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import com.ysd.ziyuanshiyongtongji.dao.IConsumeLogDao;
import com.ysd.ziyuanshiyongtongji.dao.IMemberDao;
import com.ysd.ziyuanshiyongtongji.model.ConsumeLog;
import com.ysd.ziyuanshiyongtongji.model.Member;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ConsumeLogServiceImpl
 */
@Service
public class ConsumeLogServiceImpl implements IConsumeLogService {
  @Resource
  private IMemberDao memberDao;
  @Resource
  private IConsumeLogDao consumeLogDao;
  @Transactional
    @Override
    public ConsumeLog swipingCard(Integer readRoomId, String cardNo, boolean inOrOut) {
     //根据卡号获得会员对象
      Member m=memberDao.getCardStatus(cardNo);
      ConsumeLog log=null;
      if(inOrOut){//如果是刷卡进入
        if(m.getStatus()==0){//状态正常
            //记录刷卡信息
            log =new ConsumeLog();
            log.setCardNo(cardNo);
            log.setReadRoomId(readRoomId);
           //设置离开时间为进入时间+30
            Calendar nowTime=Calendar.getInstance();
            nowTime.add(Calendar.MINUTE, 30);
            log.setOutTime(nowTime.getTime());
            log=consumeLogDao.save(log);       
            //修改会员卡状态为资源室id
            m.setStatus(readRoomId);
            memberDao.save(m);

        }else{
            //状态异常，联系管理员
        System.out.println("会员卡状态异常，请联系管理员");
        }
        }else{ //如果室刷卡离开
            //查询会员最后一次进入资源室的日志编号
            log=consumeLogDao.getMaxInTime(cardNo);
            //更新离开时间
            consumeLogDao.updateOutTime(log.getId(),new Date());
            //修改会员卡状态为0
            m.setStatus(0);
        }
        return log;//返回consumeLog对象
    }

    
}